package com.itheima.exampleAop;

import com.itheima.LockTest.SpinLock;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @PackageName: com.itheima
 * @ClassName: exampleAop
 * @Author: Renyz
 * @Date: 2021/11/4 16:10
 * @Description: //TODO
 */
//aop测试
@Aspect
@Component
public class LogAspect {

    @Pointcut("execution(public * com.itheima.time..*.*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void deBofore3(JoinPoint joinPoint) throws Exception {
        System.out.println("前置3开始开始---");
    }

    @Before("webLog()")
    public void deBofore(JoinPoint joinPoint) throws Exception {
        System.out.println("前置1开始开始---");
    }

    @Before("webLog()")
    public void deBofore1(JoinPoint joinPoint) throws Exception {
        System.out.println("前置2开始开始---");
    }

    @AfterReturning(pointcut = "webLog()")
    public void deAfter(JoinPoint joinPoint) {
        System.out.println("后置结束---" + joinPoint.getArgs().toString());
    }

    @After("webLog()")
    public void after(JoinPoint joinPoint) {
        System.out.println("最终通知");
    }

    @Around("webLog()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) {
        System.out.println("环绕前");
        try {
            Object proceed = proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            System.out.println("环绕通知出错");
        }
        System.out.println("环绕后");
    }

    @AfterThrowing(value = "webLog()", throwing = "ext")
    public void throwing(JoinPoint point, Exception ext) {
        System.out.println("异常通知");
        ext.printStackTrace();
    }

    //自旋锁测试
    public static void main(String[] args) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("1", "2");
        stringStringHashMap.put("2", "3");
        stringStringHashMap.put("3", "4");
        stringStringHashMap.put("4", "5");
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SpinLock.mylock();
                    Set<String> strings = stringStringHashMap.keySet();
                    try {
                        Iterator<String> iterator = strings.iterator();
                        while (iterator.hasNext()){
                            String next = iterator.next();
                            if (next.equals("4")) {
                                System.out.println(next);
                                iterator.remove();
                                System.out.println("map"+stringStringHashMap);
                            }else {
                                continue;
                            }
                            stringStringHashMap.put(next, "6");
                            System.out.println("map"+stringStringHashMap);
                        }

                    } catch (Exception e) {
                        System.out.println(Thread.currentThread().getName() + "异常");
                        e.printStackTrace();
                    } finally {
                        SpinLock.unMylock();
                    }
                }
            }).start();
        }
    }


}
